<?php
/* ******************************************************** */
/* Arcans Project (2010)                                    */
/* Jeu 3D en temps réel par navigateur, conception et       */
/* Développement dans la notice                             */
/* Jeu sous licence GNU GPL v3 (voir NOTICE et COPYING)     */
/* ******************************************************** */

//****************************************
//classe principale d'affichage de la page
//****************************************

class MainClass {
	//propriétés :
	private $infos;
	private $page_opt;
	private $chat;
	private $onload;
	private $messages;
	private $address;
	private $xmlinfo;
	private $header;
	private $entete;
	private $menu;
	private $page;
	private $level;
	//méthodes :
	//méthodes publiques :
	function __construct() {
		$infos_xml = new DomDocument();
		$infos_xml->load("xml/infos.xml");
		$seek_xml = $infos_xml->getElementsByTagName("seek")->item(0);
		$this->infos["seek"] = $seek_xml->getAttribute("value");
		//gestion des sessions
		if (isset($_GET["p"])) {
			$page = htmlentities($_GET["p"]);
		}
		else {
			$page = "";
		}
		switch($page) {
			//validation de l'inscription
			case "ainsv" :
				$code = htmlentities($_GET["i1"]);
				$mysql = new MysqlClass(0);
				$result = $mysql->resultFun("count", "unvalid_users", "code='".$code."'");
				if ($result == 1) {
					$mysqlinsv = new MysqlClass(2);
					$result = $mysqlinsv->resultFun("select", "unvalid_users", "code='".$code."'");
					$pwd = md5(hash("sha256", $result["users"]).hash("sha256", $result["password"]));
					$mysqlinsv->resultFun("insert", "users", "(users,password,email,level) values('".$result["users"]."','".$pwd."','".$result["email"]."',0)");
					$mysqlinsv->resultFun("delete", "unvalid_users", "code='".$code."'");
					$_SESSION["pseudo"] = $result["users"];
					$res_id = $mysql->resultFun("select","users","users='".$_SESSION["pseudo"]."'");
					$_SESSION["id"] = $res_id["id"];
					$_SESSION["level"] = 0;
					$_SESSION["panel"] = 1;
					$_SESSION["canal"] = 1;
					$this->onload = "1"; $this->level = true;
				}
				else {
					$pseudo = htmlentities($_GET["i2"]);
					$res = $mysql->resultFun("count", "users", "users='".$pseudo."'");
					if ($res == 1) { $this->onload = "3"; $this->level = false; }
					else { $this->onload = "2"; $this->level = false; }
				}
				$this->address = "i";
				break;
				//validation de la suppression du compte
			case "asupv" :
				$code = htmlentities($_GET["i1"]);
				$mysqlsupv = new MysqlClass(0);
				$res = $mysqlsupv->resultFun("count", "unvalid_users", "code='".$code."'");
				if ($res == 1) {
					//suppression des données
					$mysqlsupv2 = new MysqlClass(2);
					$mysqlsupv2->resultFun("delete", "unvalid_users", "code='".$code."'");
					//finalisation
					$this->onload = "6"; $this->level = false;
				}
				else {
					$pseudo = htmlentities($_GET["i2"]);
					$res = $mysqlsupv->resultFun("count", "users", "users='".$pseudo."'");
					if ($res == 1) { $this->onload = "5"; $this->level = false; }
					else { $this->onload = "4"; $this->level = false; }
				}
				$this->address = "i";
				break;
				//récupération du compte
			case "asupn" :
				$code = htmlentities($_GET["i1"]);
				$pseudo = htmlentities($_GET["i2"]);
				$mysqlsupn = new MysqlClass(0);
				$res = $mysqlsupn->resultFun("count", "unvalid_users", "code='".$code."'");
				if ($res == 1) {
					//récupération des données
					$res = $mysqlsupn->resultFun("select", "unvalid_users", "code='".$code."'");
					$pwd_tab_alp = array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o", "p","q", "r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
					$pwd_tab = array_rand($pwd_tab_alp, 8);
					$pwd = "";
					foreach ($pwd_tab as $elt) $pwd .= $pwd_tab_alp[$elt];
					$mysqlsupn2 = new MysqlClass(2);
					$mysqlsupn2->resultFun("insert", "users", "(users,password,email,level) values('".$pseudo."','".md5(hash("sha256", $pseudo) . hash("sha256", $pwd))."','".$res["email"]."',0)");
					$mysqlsupn2->resultFun("delete", "unvalid_users", "code='".$code."'");
					//envoi du mail
					require_once 'php/mail.php';
					mailSend($res["email"],"Arcans Project : Récupération de compte",array("validsupp",$pseudo,$pwd),$this->infos["seek"]);
					//connexion
					$_SESSION["pseudo"] = $pseudo;
					$res_id = $mysqlsupn->resultFun("select","users","users='".$_SESSION["pseudo"]."'");
					$_SESSION["id"] = $res_id["id"];
					$_SESSION["level"] = 0;
					$_SESSION["panel"] = 1;
					$_SESSION["canal"] = 1;
					//finalisation
					$this->onload = "9"; $this->level = true;
				}
				else {
					$res = $mysqlsupn->resultFun("count", "users", "users='".$pseudo."'");
					if ($res == 1) { $this->onload = "8"; $this->level = false; }
					else { $this->onload = "7"; $this->level = false; }
				}
				$this->address = "i";
				break;
				//cas de la page é adresse vierge *****************************************************
			case "" :
				$this->address = "i"; $this->onload = "0";
				if (isset($_SESSION["pseudo"])) $this->level = true;
				else $this->level = false;
				break;
				//cas par défaut **********************************************************************
			default :
				$this->address = $page; $this->onload = "0";
				if (isset($_SESSION["pseudo"])) $this->level = true;
				else $this->level = false;
				break;
		}
		if (isset($_SESSION["panel"])) {
			$this->onload = $this->onload . "," . $_SESSION["panel"] . "," . $_SESSION["canal"];
			$mysql_mail = new MysqlClass(0);
			$res_mysql = $mysql_mail->resultFun("count","dest_table","id_dest=".$_SESSION["id"]." and state=0");
			if ($res_mysql != 0) {
				$this->messages = $res_mysql;
				$this->onload = $this->onload . "," . $this->messages;
			}
			else $this->messages = "";
		}
		else $this->onload = "";
		//récupération des informations xml
		if (preg_match("/^[a]+/",$this->address)) $this->page_opt = 1;
		else {
			if ($this->address == "sep_mess") {
				$this->page_opt = 2;
				$style_xml = $infos_xml->getElementsByTagName("style_panel")->item(0);
				$this->infos["style"] = $style_xml->getAttribute("value");
			}
			else {
				$this->page_opt = 0;
				$style_xml = $infos_xml->getElementsByTagName("style_classic")->item(0);
				$this->infos["style"] = $style_xml->getAttribute("value");
			}
			$title_xml = $infos_xml->getElementsByTagName("title")->item(0);
			$this->infos["title"] = $title_xml->getAttribute("value");
			$charset_xml = $infos_xml->getElementsByTagName("charset")->item(0);
			$this->infos["charset"] = $charset_xml->getAttribute("value");
		}
		$this->initFun();
	}
	//
	function aff_xml3() {
		header('Content-Type: application/xml');
		//header('Content-Type: text/html');
		header("Expires: Mon, 26 Jul 1997 05:00:00 Paris");
		header("Last-Modified: ".date("D, d M Y H:i:s")." Paris");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");

		//$doc = DOMImplementation::createDocument('http://www.w3.org/2000/xhtml','html');
		// -> appel en statique = erreur !!!!
		$doc = new DOMDocument('1.0','utf-8');
		//DOMImplementation::createDocument('http://www.w3.org/2000/xhtml','html');
		//$doc->encoding = 'utf-8';

		$doc->formatOutput = true;

		$html = $doc->createElement("html");
		$html->setAttribute("xmlns","http://www.w3.org/1999/xhtml");
		//$html->setAttribute("xml:lang","fr");
		$doc->appendChild($html);

		$page_xml = new DomDocument();
		$page_xml->load("xml/index.xml");

		$head = $page_xml->getElementsByTagName("head")->item(0);
		$html->appendChild($doc->importNode($head, true));

		$body = $page_xml->getElementsByTagName("body")->item(0);
		$html->appendChild($doc->importNode($body, true));

		$foot = $page_xml->getElementsByTagName("footer")->item(0);
		$html->appendChild($doc->importNode($foot, true));
		
		
		/*$listeDiv = $page_xml->getElementsByTagName("div");
		
		foreach($listeDiv as $div) {
			if ($div->getAttribute("id") == "encart") {
				break;
			}
		}
		$html->appendChild($doc->importNode($div, true));*/

		echo $doc->saveXML();
	}
	//
	function affichageXML2() {
		//$this->page_opt = 1;
		if ($this->page_opt == 1) {
			//echo $this->page;
			//######## Ne fonctionne pas :-(
		}
		elseif ($this->page_opt == 2) {
			$doc = new DOMDocument();
			$doc->encoding = 'utf-8';

			$html = $doc->createElement("html");
			$html->setAttribute("xmlns","http://www.w3.org/1999/xhtml");
			$html->setAttribute("xml:lang","fr");
			$doc->appendChild($html);

			$page_xml = new DomDocument();
			$page_xml->load("xml/index.xml");

			$head = $page_xml->getElementsByTagName("head")->item(0);
			$html->appendChild($head);

			$body = $page_xml->getElementsByTagName("body")->item(0);
			$html->appendChild($body);

		}
		else {
			$doc = new DOMDocument();
			$doc->encoding = 'utf-8';
			$doc->formatOutput = true;

			$html = $doc->createElement("html");
			$html->setAttribute("xmlns","http://www.w3.org/1999/xhtml");
			$html->setAttribute("xml:lang","fr");
			$doc->appendChild($html);

			$page_xml = new DomDocument();
			$page_xml->load("xml/index.xml");

			$head = $page_xml->getElementsByTagName("head")->item(0);
			$head = $doc->importNode($head, true);
			$html->appendChild($head);

			$body = $page_xml->getElementsByTagName("body")->item(0);
			$body = $doc->importNode($body, true);
			$html->appendChild($body);
		}




		//ob_end_clean(); //Permet de ne pas prendre en compte toutes les erreurs d'espaces en fin de ligne
		header('Content-type: text/xml');
		// Rendre Joli ;)
		$doc->formatOutput = true;

		// Afficher le document XML
		echo $doc->saveXML();
		// Sauver le document XML sous le nom simple.xml
		//$doc->save('simple.xml');

		// Yeah on a fini!

	}
	private function initFun() {
		if ($this->page_opt == 1) {
			require_once 'php/ajax.php';
			require_once 'php/mail.php';
			$this->page = AjaxReponseClass::initAjaxFun($this->address, $this->infos["seek"]);
		}
		elseif ($this->page_opt == 2) {
			require_once 'php/entete.php';
			if ($_SESSION["panel"] == 4) $titre = "Messagerie";
			elseif ($_SESSION["panel"] == 5) $titre = "Chat";
			$this->header = new HeaderClass('panel','', $titre, $this->infos);
		}
		else {
			require_once 'php/entete.php';
			require_once 'php/menu.php';
			require_once 'php/page.php';
			require_once 'php/xml.php';
			$this->xmlinfo = new ObjXmlClass($this->level, $this->address);
			$this->header = new HeaderClass('classic',$this->xmlinfo->infoHead(), $this->xmlinfo->pageTitle(), $this->infos);
			$this->entete = new EnteteClass();
			$this->menu = new MenuClass($this->xmlinfo->infoMenu());
			$this->page = new PageClass($this->xmlinfo->structText());
		}
	}
}
//******************************
//script de gestion des requétes
//******************************
session_start();
require_once 'php/mysql.php';
$rootObj = new MainClass();
//$rootObj->affichage();
//$rootObj->affichageXML2();
$rootObj->aff_xml3();
?>